package com.neoCalf.neoGroup.config;


import com.neoCalf.neoCommon.enums.ResultCode;
import com.neoCalf.neoCommon.exception.AuthException;
import com.neoCalf.neoCommon.exception.BusinessException;
import com.neoCalf.neoCommon.model.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2023/7/7 11:54
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public R<String> handleException(Exception e) {
        log.error("系统异常", e);
        return R.fail(ResultCode.SystemError);
    }

    @ExceptionHandler(BusinessException.class)
    public R<String> BusinessException(BusinessException e) {
        log.error(e.getMessage());
        return R.fail(e.getResultCode());
    }

    @ExceptionHandler(AuthException.class)
    public R<String> AuthException(AuthException e) {
        log.error(e.getMessage());
        return R.fail(e.getResultCode());
    }


    @ExceptionHandler(NoHandlerFoundException.class)
    public R<String> NoHandlerFoundException(NoHandlerFoundException e) {
        log.error("请求路径不存在 {}", e.getMessage());
        return R.fail(ResultCode.NotFindError);
    }

    @ExceptionHandler(IllegalStateException.class)
    public R<String> IllegalStateException(IllegalStateException e) {
        log.error("参数错误 {}", e.getMessage());
        return R.fail(ResultCode.CODE_10017);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public R<String> HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("参数错误 {}", e.getMessage());
        return R.fail(ResultCode.CODE_10017);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public R<String> IllegalArgumentException(IllegalArgumentException e) {
        log.error("参数错误 {}", e.getMessage());
        return R.fail(ResultCode.CODE_10017);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R<String> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("参数错误 {}", e.getMessage());
        return R.fail(ResultCode.CODE_10017);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public R<String> ConstraintViolationException(ConstraintViolationException e) {
        log.error("参数错误 {}", e.getMessage());
        return R.fail(ResultCode.CODE_10017);
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public R<String> MissingServletRequestParameterException(MissingServletRequestParameterException e) {
        log.error("参数错误 {}", e.getMessage());
        return R.fail(ResultCode.CODE_10017);
    }
}
